package com.laowang.project.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.MyCyhImg;
import com.laowang.project.system.domain.MyCyhShop;
import com.laowang.project.system.service.MyCyhImgService;
import com.laowang.project.system.service.MyCyhShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCyhShopCattle;
import com.laowang.project.system.service.MyCyhShopCattleService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 店铺Controller
 * 
 * @author laowang
 * @date 2024-04-11
 */
@Api(tags = "店铺接口")
@RestController
@RequestMapping("/shopCattle")
public class MyCyhShopCattleController extends BaseController {
    @Autowired
    private MyCyhShopCattleService myCyhShopCattleService;

    @Autowired
    private MyCyhImgService myCyhImgService;


    @Autowired
    private MyCyhShopService myCyhShopService;

    /**
     * 小程序端  新增铺
     */
    @ApiOperation(value = "小程序端  新增铺",notes = "")
    @PostMapping("/addShop")
    public AjaxResult addShop(@RequestBody MyCyhShopCattle myCyhShopCattle) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();

        Integer number = myCyhShopCattle.getNumber();
        if (number == null){
            throw new CustomException("店铺数量不能为空");
        }if (myCyhShopCattle.getPlanId() == null){
            throw new CustomException("方案不能为空");
        }
        BigDecimal amount = myCyhShopCattle.getAmount();
        if (amount == null){
            throw new CustomException("方案金额为空");
        }
        MyCyhImg myCyhImg = myCyhImgService.selectMyCyhImgById(myCyhShopCattle.getPlanId());
        if (number ==1){

            if (amount.compareTo(myCyhImg.getBalance()) != 0){
                throw new CustomException("金额计算有误");
            }

        }else  if (number >1){
            BigDecimal multiply = new BigDecimal((number-1) * 0.2).multiply(myCyhImg.getBalance()).add(myCyhImg.getBalance());
            if (amount.compareTo(multiply.setScale(1,BigDecimal.ROUND_DOWN)) !=0){
                throw new CustomException("金额计算有误");
            }

        }

        myCyhShopCattle.setCreated(new Date());
        myCyhShopCattle.setUserId(mid);
        myCyhShopCattle.setPay(0);//未支付
        myCyhShopCattleService.insertMyCyhShopCattle(myCyhShopCattle);
        return AjaxResult.success("操作成功",myCyhShopCattle.getId());
    }


    /**
     * 小程序端  新增铺
     */
    @ApiOperation(value = "小程序端  我的店铺",notes = "")
    @PostMapping("/myShop")
    public AjaxResult myShop(MyCyhShopCattle myCyhShopCattle) {
        List<MyCyhShopCattle> list =new ArrayList<>();
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();

        myCyhShopCattle.setUserId(mid);
        List<MyCyhShopCattle> myCyhShopCattles = myCyhShopCattleService.selectMyCyhShopCattleList(myCyhShopCattle);
        for (MyCyhShopCattle cyhShopCattle : myCyhShopCattles) {
            MyCyhShop myCyhShop = new MyCyhShop();
            myCyhShop.setShopCattleId(cyhShopCattle.getId());
            List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopListWeb(myCyhShop);
            cyhShopCattle.setMyCyhShopList(myCyhShops);
            list.add(cyhShopCattle);
        }

        return AjaxResult.success("操作成功",list);
    }

    @ApiOperation(value = "商家端  修改铺信息",notes = "")
    @PostMapping("/edit")
    public AjaxResult editOne(@RequestBody MyCyhShopCattle myCyhShopCattle) {
        Integer id = myCyhShopCattle.getId();
        if (id== null) return AjaxResult.error("铺id 不能为空");
        myCyhShopCattleService.updateMyCyhShopCattle(myCyhShopCattle);
        return AjaxResult.success();
    }

    @ApiOperation(value = "车友会  根据铺查询分店",notes = "")
    @GetMapping("/shopDetail")
    public AjaxResult shopNumber(Integer id) {

        MyCyhShop myCyhShop = new MyCyhShop();
        myCyhShop.setShopCattleId(id);
        List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopListWeb(myCyhShop);

        return AjaxResult.success("操作成功",myCyhShops);
    }


    /**
     * 查询店铺列表
     */
    @ApiOperation(value = "查询店铺列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:list')")
    @GetMapping("/list")
        public TableDataInfo list(MyCyhShopCattle myCyhShopCattle) {
        startPage();
        List<MyCyhShopCattle> list = myCyhShopCattleService.selectMyCyhShopCattleList(myCyhShopCattle);
        return getDataTable(list);
    }
    
    /**
     * 导出店铺列表
     */
    @ApiOperation(value = "导出店铺列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:export')")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCyhShopCattle myCyhShopCattle) {
        List<MyCyhShopCattle> list = myCyhShopCattleService.selectMyCyhShopCattleList(myCyhShopCattle);
        ExcelUtil<MyCyhShopCattle> util = new ExcelUtil<MyCyhShopCattle>(MyCyhShopCattle.class);
        return util.exportExcel(list, "shopCattle");
    }

    /**
     * 获取店铺详细信息
     */
    @ApiOperation(value = "获取店铺详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCyhShopCattleService.selectMyCyhShopCattleById(id));
    }

    /**
     * 新增店铺
     */
    @ApiOperation(value = "新增店铺",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:add')")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCyhShopCattle myCyhShopCattle) {
        return toAjax(myCyhShopCattleService.insertMyCyhShopCattle(myCyhShopCattle));
    }

    /**
     * 修改店铺
     */
    @ApiOperation(value = "修改店铺",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:edit')")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCyhShopCattle myCyhShopCattle) {
        return toAjax(myCyhShopCattleService.updateMyCyhShopCattle(myCyhShopCattle));
    }

    /**
     * 删除店铺
     */
    @ApiOperation(value = "删除店铺",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCattle:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCyhShopCattleService.deleteMyCyhShopCattleByIds(ids));
    }
}
